/*
 * @Date: 2025-07-16 16:15:23
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-08-04 10:48:59
 * @FilePath: \corpus\src\pages\AuthKnowledge\ApplyAuthKnowledge\index.tsx
 */
import { Form, Input, Modal, Select } from 'antd';
import hook from '@/pages/Home/hooks/useHooks';
import React, { useEffect } from 'react';
import { useModel } from '@umijs/max';
import { DeptTypeEnum } from '@/enum';

const ApplyAuthKnowledge: React.FC = () => {
  const { deptList, getDept, chooseDeptList } = useModel('useGlobal', (global) => {
    return {
      deptList: global.deptList,
      chooseDeptList: global.chooseDeptList,
      getDept: global.getDept,
    };
  });
  const {
    applyVisible,
    setDetail,
    type,
    disabled,
    applyKnowledgeData,
    setApplyVisible,
    formApply,
    detail,
  } = hook.useContainer();

  const { initialState } = useModel('@@initialState');

  const { currentUser } = initialState || {};
  /**提交 */
  const handleSubmit = () => {
    formApply.validateFields().then((vals) => {
      const method = applyKnowledgeData;
      method.run({
        ...vals,
        type: type === 'gr' ? DeptTypeEnum.GR_TO_BM : type === 'bm' ? DeptTypeEnum.BM_TO_GG : '',
      });
    });
  };
  useEffect(() => {
    if (applyVisible && detail) {
      formApply.setFieldsValue({
        ...detail,
        knowledgeName: detail.name,
        knowledgeId: detail.id,
        knowledgeDeptCode: detail.deptCode,
        knowledgeDeptName: detail.deptName,
      });
      formApply.setFieldValue('applyAccountId', currentUser?.accountId);
      formApply.setFieldValue('applyUserName', currentUser?.nickName);
      if (type === 'gr' && detail.submitParentDeptCode) {
        getDept.run(detail.submitParentDeptCode);
      }
    }
  }, [detail, applyVisible]);
  return (
    <Modal
      width={600}
      title="知识库授权申请"
      open={applyVisible}
      onCancel={() => {
        setApplyVisible(false);
        setDetail({});
        formApply.resetFields();
      }}
      onOk={handleSubmit}
      confirmLoading={applyKnowledgeData.loading}
      okButtonProps={{ disabled: disabled }}
    >
      <Form labelCol={{ span: 5 }} form={formApply}>
        <Form.Item name="knowledgeId" hidden />
        <Form.Item name="applyAccountId" hidden />
        <Form.Item name="submitDeptName" hidden />

        <Form.Item name="knowledgeDeptName" hidden />
        <Form.Item name="submitParentDeptName" hidden />

        <Form.Item label={'申请人'} name="applyUserName" rules={[{ required: true }]}>
          <Input disabled placeholder="请输入" />
        </Form.Item>

        <Form.Item label={'所属部门'} name="knowledgeDeptCode" rules={[{ required: true }]}>
          <Select
            fieldNames={{ label: 'deptName', value: 'deptCode' }}
            options={currentUser?.deptList}
            disabled
          ></Select>
        </Form.Item>

        <Form.Item label={'知识库名称'} name="knowledgeName" rules={[{ required: true }]}>
          <Input disabled placeholder="请输入" />
        </Form.Item>

        <Form.Item label={'知识库描述'} name="knowledgeDescribe" rules={[{ required: true }]}>
          <Input.TextArea disabled placeholder="请输入" />
        </Form.Item>

        {type === 'gr' && (
          <>
            <Form.Item
              label={'一级单位名称'}
              name="submitParentDeptCode"
              rules={[{ required: true }]}
            >
              <Select
                fieldNames={{ label: 'govShortName', value: 'organizationCode' }}
                options={deptList}
                placeholder="请选择"
                disabled={disabled}
                onChange={(arg: string, arg2: API.DeptType) => {
                  formApply.setFieldValue('submitParentDeptName', arg2.govShortName);
                  formApply.setFieldValue('submitDeptCode', '');
                  formApply.setFieldValue('submitDeptName', '');
                  getDept.run(arg);
                }}
              ></Select>
            </Form.Item>

            <Form.Item label={'二级单位名称'} name="submitDeptCode" rules={[{ required: true }]}>
              <Select
                fieldNames={{ label: 'govShortName', value: 'organizationCode' }}
                options={chooseDeptList}
                disabled={disabled}
                placeholder="请选择"
                onChange={(arg: string, arg1: API.DeptType) => {
                  formApply.setFieldValue('submitDeptName', arg1.govShortName);
                }}
              ></Select>
            </Form.Item>
          </>
        )}

        <Form.Item label={'申请人电话'} name="applyUserPhone" rules={[{ required: true }]}>
          <Input disabled={disabled} placeholder="请输入" />
        </Form.Item>
        <Form.Item label={'备注'} name="remark" rules={[{ required: true }]}>
          <Input.TextArea disabled={disabled} placeholder="请输入" />
        </Form.Item>
      </Form>
    </Modal>
  );
};
export default ApplyAuthKnowledge;
